exports.agentIndex = (req,res) => {

    var os_id = req.headers.os_id;     //当前节点ID

    var resultData = {};

    //获取该节点下的代理商
    function searchAgent(cb){

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.organizationStructureExtensionInfoSearch.Request_Api_Url + "/no/os_master_type-0/os_fid/"+os_id+"/os_is_delete/0/skip/0/limit/999",
                method : 'POST',
                data : {
                    fields : `
                        os_id,
                        os_name,
                        osei_phone
                    `
                }
            },
            function (data) {

                resultData.agentData = data.result.data;

                cb();
                
            }
        )

    }

    //查询代理的成交数以及销售总额
    function searchAgentOrder(cb){


        F.ParallelEach(resultData.agentData,function(item,index,callback){

            //记录每个代理商的总阅读数
            var agentReadCount = 0;

            //获取当前代理下的所有人员ID
            R.SEND_HTTP(
                req,
                res,
                {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_master_osid/" + item.os_id,
                    method : 'POST',
                    data : {
                        fields : `
                            u_id,
                            u_nickname,
                            u_read_count
                        `
                    }
                },
                function (data) {
                
                    var userArr = data.result.data;

                    //计算当前代理上下销售的总阅读数
                    for(var i=0;i<userArr.length;i++){
                        agentReadCount += parseInt(userArr[i].u_read_count);
                    }

                    item.agentReadCount = agentReadCount;

                    //拼接为in语句所需数据
                    var userInStr = "";
                    for(var i=0;i<userArr.length;i++){
                        userInStr += userArr[i].u_id + ","
                    }
                    userInStr = userInStr.substring(0,userInStr.length - 1);

                    //获取该代理下所有销售的订单数据
                    R.SEND_HTTP(
                        req,
                        res,
                        {
                            url: config.external_server.apiAddress + "/organization/search_art_product_order/apo_status/1/in/apo_invite_code-" + userInStr,
                            method : 'POST',
                            data   : {
                                fields : `apo_real_pay`
                            }
                        },
                        function (data) {

                            //计算成交总数 和 成交金额
                            var buyCount = data.result.data.length;
                            var buyMoney = 0.0;

                            for(var i=0;i<data.result.data.length;i++){
                                buyMoney += parseFloat(data.result.data[i].apo_real_pay);
                            }
            
                            item.buyCount = buyCount;
                            item.buyMoney = buyMoney.toFixed(2);
                           
                            callback();
                            
                        }
                    )
                    
                }
            )

        },cb);

    }

    //计算所有代理的总成交数 和 成交金额
    function searchAllAgentOrder(cb){

        var allBuyCount  = 0;
        var allBuyMoney  = 0.0;
        var allReadCount = 0;

        for(var i=0;i<resultData.agentData.length;i++){

            allBuyCount  += parseInt(resultData.agentData[i].buyCount);
            allBuyMoney  += parseFloat(resultData.agentData[i].buyMoney);
            allReadCount += parseInt(resultData.agentData[i].agentReadCount);

        }

        resultData.allBuyCount  = allBuyCount;
        resultData.allBuyMoney  = allBuyMoney;
        resultData.allReadCount = allReadCount;

        cb();

    }

    F.Serial([searchAgent,searchAgentOrder,searchAllAgentOrder],() => {
        RES.successResponse(res,resultData);
    })

}